package com.numberONe.controller.patientcase;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.numberONe.annotation.SystemLog;
import com.numberONe.controller.index.BaseController;
import com.numberONe.entity.PatientFormMap;
import com.numberONe.entity.UserFormMap;
import com.numberONe.exception.SystemException;
import com.numberONe.mapper.PatientMapper;
import com.numberONe.plugin.PageView;
import com.numberONe.util.Common;

/**
 * 
 * @author zy
 * @version 100
 */
@Controller
@RequestMapping("/patient/")
public class PatientController extends BaseController{
	@Inject
	private PatientMapper patientMapper;
	
	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		String[] flag = getParaValues("flag");
		model.addAttribute("res", findByRes());
		if(flag==null) {
			return Common.WEB_PATH + "/patient/list";
		}else {
			return Common.WEB_PATH + "/patient/sel";
		}
	}
	
	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage( String pageNow,
			String pageSize,String column,String sort,HttpSession session) throws Exception {
		PatientFormMap patientFormMap = getFormMap(PatientFormMap.class);
		UserFormMap userFormMap = (UserFormMap)session.getAttribute("userSession");
		//TODO 如果是管理员则能查看所有患者信息
		String roleName = patientMapper.findUserRoleName(userFormMap.getInt("id"));
		if(!"管理员".equals(roleName)){
			patientFormMap.set("user_id",userFormMap.getInt("id"));
		}
		String create_time = patientFormMap.getStr("create_time");
		if(create_time==null||"".equals(create_time)) {
		}else {
			String[] dates = create_time.split(" - ");
			patientFormMap.put("begindate", dates[0]);
			patientFormMap.put("enddate", dates[1]);
		}
		patientFormMap=toFormMap(patientFormMap, pageNow, pageSize,patientFormMap.getStr("orderby"));
		patientFormMap.put("column", column);
		patientFormMap.put("sort", sort);
        pageView.setRecords(patientMapper.findPatientPage(patientFormMap));//不调用默认分页,调用自已的mapper中findPatientPage
        return pageView;
	}

	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.WEB_PATH + "/patient/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@SystemLog(module="诊疗管理",methods="患者信息-新增患者")
	@Transactional(readOnly=false)
	public String addEntity(HttpSession session){
		try {
			PatientFormMap patientFormMap = getFormMap(PatientFormMap.class);
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
			SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String patientNo = df.format(new Date());//根据日期生成患者编号
			patientFormMap.set("patient_number",patientNo);
			patientFormMap.set("create_time", dt.format(new Date()));
			UserFormMap userFormMap = (UserFormMap)session.getAttribute("userSession");
			patientFormMap.set("user_id",userFormMap.getInt("id"));
			patientMapper.addEntity(patientFormMap);//新增后返回新增信息
		} catch (Exception e) {
			 throw new SystemException("添加患者信息异常");
		}
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("save")
	@SystemLog(module="诊疗管理",methods="患者信息-新增患者")
	@Transactional(readOnly=false)
	public PatientFormMap save(HttpSession session){
		try {
			PatientFormMap patientFormMap = getFormMap(PatientFormMap.class);
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
			SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String patientNo = df.format(new Date());//根据日期生成患者编号
			patientFormMap.set("patient_number",patientNo);
			patientFormMap.set("create_time", dt.format(new Date()));
			UserFormMap userFormMap = (UserFormMap)session.getAttribute("userSession");
			patientFormMap.set("user_id",userFormMap.getInt("id"));
			patientMapper.addEntity(patientFormMap);//新增后返回新增信息
			patientFormMap.set("flag", "success");
			return patientFormMap;
		} catch (Exception e) {
			 throw new SystemException("添加患者信息异常");
		}
	}

	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional(readOnly=false)
	@SystemLog(module="诊疗管理",methods="患者信息-删除患者")
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			patientMapper.deleteByAttribute("id", id, PatientFormMap.class);
		}
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("patient", patientMapper.findbyFrist("id", id, PatientFormMap.class));
		}
		return Common.WEB_PATH + "/patient/edit";
	}
	
	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly=false)
	@SystemLog(module="诊疗管理",methods="患者信息-修改患者")
	public String editEntity() throws Exception {
		PatientFormMap patientFormMap = getFormMap(PatientFormMap.class);
		SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		patientFormMap.set("update_time", dt.format(new Date()));
		patientMapper.editEntity(patientFormMap);
		return "success";
	}

	/**
	 * 验证账号是否存在
	 * @param zy
	 * @return
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String phoneNum) {
		PatientFormMap number = patientMapper.findbyFrist("patient_phone", phoneNum, PatientFormMap.class);
		if (number == null) {
			return true;
		} else {
			return false;
		}
	}

}
